/**
 * *其他出库单中：选择单据按钮事件 lvruinan
 */
function chooseCL(){
	// 获取出库类型
	var DJ_TYPE=$('#qtc_addWinForm').find('select[qucln=DJ_TYPE]').val();
	// 获取库房
	var STOCK_ID=$('#qtc_addWinForm').find('input[qucln=STOCK_ID]').val();
	
	// 出库类型和库房不能为空
	if (DJ_TYPE=='') {
		$.messager.alert('提示','请选择出库类型！');
		return;
	}
	
	if (STOCK_ID==''){
		$.messager.alert('提示','请选择单据库房！');
		return;
	}
	var dgUrl=_m_url+'fxa/findOtherCk';
	var queryParams={'dsId':'ds_yz_cl_select1','DJ_TYPE':DJ_TYPE,'STOCK_ID':STOCK_ID};
	
	$.ajax({
		type:'post',
		url:_m_url+'fxa/findOtherCk',
		data:{'dsId':'ds_yz_cl_select1','DJ_TYPE':DJ_TYPE,'STOCK_ID':STOCK_ID},
		success:function(data){
			// 根据现成的数据展现窗口
			$.ajax({
				type:'post',
				url:_m_url+'fxa/UpdateDefaultOtherck',
				data:{'dsId':'ds_yz_cl_select1','STOCK_ID':STOCK_ID},
				success:function(data){
				} 
			});
			_data_qtc('qts','ds_yz_cl_select1','qts_sure1','77','',data,dgUrl,queryParams);
		}
	});
}


/**
 * *其他出库单中：选择单据按钮弹框事件
 */
function _data_qtc(prefix,dsId,butnId,type,ainitFunc,dataList,dgUrl,queryParams){
	var width = document.documentElement.clientWidth*0.97;
	var height = window.parent.innerHeight-270;
	if($('#'+prefix+'_DATA_WIN').length>0){
		$('#'+prefix+'_DATA_PARENT_PANEL').window('close');
		var obj = document.getElementById(prefix+"_DATA_WIN");
		$('#'+prefix+'_DATA_PARENT_PANEL').remove();
		document.body.removeChild(obj);
	}
	var div_box = document.createElement("DIV");
	div_box.id = prefix+"_DATA_WIN";
	div_box.height = 100;
	document.body.appendChild(div_box);
	$('#'+prefix+'_DATA_WIN').load(_m_url+'singlePages/genFun',
			{'dsId':dsId,'butnId':butnId,'type':type,'staticWebUrl':'','initFunc':'','jspWebUrl':'/model/otherGen/odatalistD'},
			function(){
				$.parser.parse('#'+prefix+'_DATA_DG_PANEL');
				$('#'+prefix+'_DATA_PARENT_PANEL').window({  
					width:document.documentElement.clientWidth,  
					height:document.documentElement.clientHeight,  
					draggable:false,
					top:'0',
					left:'0',
					modal:true,
					resizable:false,
					closable:false,
					collapsible:false,
					maximizable:false,
					minimizable:false
				});  
				$('#'+prefix+'_DATA_PARENT_PANEL').window('open');
				// 加载字段 变不可写 填充数据
				var divName = 'DATA_DATALIST';
				// 加载字段
				$.ajax({
					type:'post',
					url:_m_url+'framework/loadColumnsU',
					data:{'colId':dsId,'type':type},
					success:function(data){
						if(data==""){
							$.messager.alert('提示',"您长时间未操作，系统已经自动退出!，请重新登陆");
						}else{
							var options = jointDGColumn(data[0]);
							var fzClns = options.frozenColumns;
							options.frozenColumns = [[{field:'kk',checkbox:true}]];
							for(var i = 0; i < fzClns[0].length; i ++){
								options.frozenColumns[0].push(fzClns[0][i]);
							}
							// 获取div的宽度和高度
							$('#'+prefix+'_DATA_DATALIST').datagrid({
								iconCls:'icon-save',
								pageNumber:1,
								url:dgUrl,
								queryParams:queryParams,
								height:height,
								width:width,
								striped: true,		
								collapsible:true,
								autoRowHeight: false,		
								sortOrder: 'desc',
								remoteSort: false,
								pageList:[300,500,1000],
								columns:options.columns,
								frozenColumns:options.frozenColumns,
								pagination:true,
								rownumbers:true,
								singleSelect:false,
								rowStyler:function(index,row){  
									return window.parent.getRowStyler(index,row,data[0]);
								},
								onRowContextMenu:function(e, rowIndex, rowData){// 右键菜单
								    $('#mm').menu('show', {
								        left:e.pageX,
								        top:e.pageY
								    });
								},
								onClickRow:function(rowIndex,rowData){
									// 获取到currentIndex
									var currentIndex=$(this).attr("currentIndex");
									if(rowIndex!=currentIndex){
										var colnName=$(this).attr("colnName");
										$(this).datagrid("endEdit",currentIndex)
										$(this).datagrid("beginEdit",rowIndex);
										$(this).attr("currentIndex",rowIndex);
										var cRowStyle=$($($(this).datagrid("getEditor",{index:rowIndex,field:colnName}).target[0]).parent("td").parents("tr[datagrid-row-index]")[0]).attr("class");
										var amountObj=$($(this).datagrid("getEditor",{index:rowIndex,field:colnName}).target[0]).parent("td").find("#editablebox");
										// 清空数量的值
										$($(this).datagrid("getEditor",{index:rowIndex,field:colnName}).target[0]).parent("td").find("input").val("");
										amountObj.focus();
										$($(this).datagrid("getEditor",{index:rowIndex,field:colnName}).target[0]).parent("td").find("#editablebox").css("background-color","#C9ECFE");
									}
								},
								onLoadSuccess:function(data){
									var colnName="H_AMOUNT";
									{if(dsId=='ds_bs_product2'||dsId=='ds_bs_product4'){
										colnName="AMOUNT";
									}
									if(data.rows.length>0)
										$(this).datagrid('beginEdit', 0);
										var ed =$(this).datagrid('getEditor', {index:0,field:colnName});
										if(ed!=null&&ed!=undefined){
											$(this).attr("colnName",colnName);
											$(this).attr("currentIndex",0);
											var amountObj=$($(this).datagrid("getEditor",{index:0,field:colnName}).target[0]).parent("td").find("#editablebox");
											$(amountObj).keydown(function() {
												$('#'+prefix+'_DATA_DATALIST').datagrid('selectRow',0);
											});
											$(amountObj).focus();
											$(amountObj).css("background-color","#C9ECFE");
										}else{
											console.log("===========如果该列表界面没有光标定位，那么请找到对应的数量列，并告诉开发配置");
										}
									}
									$('#qts_commDataLeftD').find('input[qucln=CLSTOCK]').next().attr('disabled',"true");
								},
							});
							$('#'+prefix+'_DATA_DATALIST').datagrid("keyCtr_cp");       // 绑定datagrid
																						// 的键盘事件
							if(ainitFunc!='')eval(ainitFunc).call(this,prefix);
						}
					}
				});
			}
		);
}


/**
 * 弹窗的确定按钮
 * 
 * @return
 */
function _qts_sure1(){
	// 关闭弹窗
	$('#qts_DATA_PARENT_PANEL').window('close');
	// 原有明细数据
	var oldRows = $('#qtc_addItemDatagrid').datagrid('getRows');
	// 获取选择的数据
	var selRows = $('#qts_DATA_DATALIST').datagrid('getSelections');
	
	// 关闭弹出窗口的editor
	for(var i=0;i<selRows.length;i++){
		var index=$('#qts_DATA_DATALIST').datagrid("getRowIndex",selRows[i]);
		$('#qts_DATA_DATALIST').datagrid('endEdit',index);
	}
	var amount;
	var price;
	for ( var j = 0; j < selRows.length; j++) {
		if (selRows[j].CL_UNIT == 'cljlUnit.003') { // 张
			amount = parseFloat(selRows[j].ZHANG_AMOUNT);
			price=parseFloat(selRows[j].CLPRICE_Z);
		} else if (selRows[j].CL_UNIT == 'cljlUnit.002') { // 令
			amount = parseFloat(selRows[j].L_AMOUNT);
			price=parseFloat(selRows[j].CLPRICE_L);
		}
		
		$('#qtc_addItemDatagrid').datagrid('appendRow', {
			STOCK_ID:selRows[j].CLSTOCK,
			VENDOR_ID:selRows[j].VENDOR_ID,
			CL_ID:selRows[j].CLID,
            CLNAME:selRows[j].CLNAME,
			CL_WEIGHT:selRows[j].CL_WEIGHT,
			CL_STANDARD:selRows[j].CL_STANDARD,	
			CL_UNIT:selRows[j].CL_UNIT,
			H_AMOUNT:selRows[j].H_AMOUNT,
			CL_HETONGHAO:selRows[j].CLHTH,
            XT_ID:selRows[j].XT_ID,
            YINCI:selRows[j].YINCI,
			PRICE:price,
			AMOUNT:amount,
			TAXRATE:'17%',
			TAXMONEY:selRows[j].H_AMOUNT==""||selRows[j].H_AMOUNT==null||selRows[j].H_AMOUNT==undefined?0:((price*parseFloat(selRows[j].H_AMOUNT))-(price*parseFloat(selRows[j].H_AMOUNT)/1.17)),
			MX_HMONEY:selRows[j].H_AMOUNT==""||selRows[j].H_AMOUNT==null||selRows[j].H_AMOUNT==undefined?0:price*parseFloat(selRows[j].H_AMOUNT),
			MX_BHMONEY:selRows[j].H_AMOUNT==""||selRows[j].H_AMOUNT==null||selRows[j].H_AMOUNT==undefined?0:price*parseFloat(selRows[j].H_AMOUNT)/1.17
		});
	}
	oldRows = $('#qtc_addItemDatagrid').datagrid('getRows');
	var total1 = 0;
	var total2 = 0;
	var total3 = 0;
	var total4 = 0;
	for (var i = 0; i < oldRows.length; i++) {
		total1 += (oldRows[i]['H_AMOUNT'] == ""
			|| oldRows[i]['H_AMOUNT'] == null
			|| oldRows[i]['H_AMOUNT'] == undefined ? 0
			: parseFloat(oldRows[i]['H_AMOUNT']));
	
	total2 += (oldRows[i]['MX_HMONEY'] == ""
			|| oldRows[i]['MX_HMONEY'] == null
			|| oldRows[i]['MX_HMONEY'] == undefined ? 0
			: parseFloat(oldRows[i]['MX_HMONEY']));
	
	total3 += (oldRows[i]['MX_BHMONEY'] == ""
			|| oldRows[i]['MX_BHMONEY'] == null
			|| oldRows[i]['MX_BHMONEY'] == undefined ? 0
			: parseFloat(oldRows[i]['MX_BHMONEY']));
	
	total4 += (oldRows[i]['TAXMONEY'] == ""
		|| oldRows[i]['TAXMONEY'] == null
		|| oldRows[i]['TAXMONEY'] == undefined ? 0
		: parseFloat(oldRows[i]['TAXMONEY']));
    }
	
	$('#qtc_addWinForm').find('input[qucln=TOTAL_AMOUNT]').val(parseFloat(total1).toFixed(2));
	$('#qtc_addWinForm').find('input[qucln=TOTAL_AMOUNT]').attr("chan", "T");
	$('#qtc_addWinForm').find('input[qucln=TOTAL_AMOUNT]').next().val(parseFloat(total1).toFixed(2));

	$('#qtc_addWinForm').find('input[qucln=MX_HMONEYSUM]').val(
			parseFloat(total2).toFixed(2));
	$('#qtc_addWinForm').find('input[qucln=MX_HMONEYSUM]').attr("chan", "T");
	$('#qtc_addWinForm').find('input[qucln=MX_HMONEYSUM]').next().val(
			parseFloat(total2).toFixed(2));

	$('#qtc_addWinForm').find('input[qucln=MX_BHMONEYSUM]').val(
			parseFloat(total3).toFixed(2));
	$('#qtc_addWinForm').find('input[qucln=MX_BHMONEYSUM]').attr("chan", "T");
	$('#qtc_addWinForm').find('input[qucln=MX_BHMONEYSUM]').next().val(
			parseFloat(total3).toFixed(2));

	$('#qtc_addWinForm').find('input[qucln=TAXMONEY]').val(
			parseFloat(total4).toFixed(2));
	$('#qtc_addWinForm').find('input[qucln=TAXMONEY]').attr("chan", "T");
	$('#qtc_addWinForm').find('input[qucln=TAXMONEY]').next().val(
			parseFloat(total4).toFixed(2));
	
}

/**
 * 其他出库单的删除明细的按钮
 */
function deleteCL(obj,dgUrl,queryParams){
	// 获取选中的行
	var oldRows = $('#qtc_addItemDatagrid').datagrid('getSelections');
	for(var i=0;i<oldRows.length;i++){
		var rowIndex= $('#qtc_addItemDatagrid').datagrid('getRowIndex',oldRows[i]);
		$('#qtc_addItemDatagrid').datagrid('deleteRow',rowIndex);
	}
	
	oldRows = $('#qtc_addItemDatagrid').datagrid('getRows');
	var total1 = 0;
	var total2 = 0;
	var total3 = 0;
	var total4 = 0;
	for (var i = 0; i < oldRows.length; i++) {
		total1 += (oldRows[i]['H_AMOUNT'] == ""
			|| oldRows[i]['H_AMOUNT'] == null
			|| oldRows[i]['H_AMOUNT'] == undefined ? 0
			: parseFloat(oldRows[i]['H_AMOUNT']));
	
	total2 += (oldRows[i]['MX_HMONEY'] == ""
			|| oldRows[i]['MX_HMONEY'] == null
			|| oldRows[i]['MX_HMONEY'] == undefined ? 0
			: parseFloat(oldRows[i]['MX_HMONEY']));
	
	total3 += (oldRows[i]['MX_BHMONEY'] == ""
			|| oldRows[i]['MX_BHMONEY'] == null
			|| oldRows[i]['MX_BHMONEY'] == undefined ? 0
			: parseFloat(oldRows[i]['MX_BHMONEY']));
	
	total4 += (oldRows[i]['TAXMONEY'] == ""
		|| oldRows[i]['TAXMONEY'] == null
		|| oldRows[i]['TAXMONEY'] == undefined ? 0
		: parseFloat(oldRows[i]['TAXMONEY']));
    }
	
	$('#qtc_addWinForm').find('input[qucln=TOTAL_AMOUNT]').val(parseFloat(total1).toFixed(2));
	$('#qtc_addWinForm').find('input[qucln=TOTAL_AMOUNT]').attr("chan", "T");
	$('#qtc_addWinForm').find('input[qucln=TOTAL_AMOUNT]').next().val(parseFloat(total1).toFixed(2));

	$('#qtc_addWinForm').find('input[qucln=MX_HMONEYSUM]').val(
			parseFloat(total2).toFixed(2));
	$('#qtc_addWinForm').find('input[qucln=MX_HMONEYSUM]').attr("chan", "T");
	$('#qtc_addWinForm').find('input[qucln=MX_HMONEYSUM]').next().val(
			parseFloat(total2).toFixed(2));

	$('#qtc_addWinForm').find('input[qucln=MX_BHMONEYSUM]').val(
			parseFloat(total3).toFixed(2));
	$('#qtc_addWinForm').find('input[qucln=MX_BHMONEYSUM]').attr("chan", "T");
	$('#qtc_addWinForm').find('input[qucln=MX_BHMONEYSUM]').next().val(
			parseFloat(total3).toFixed(2));

	$('#qtc_addWinForm').find('input[qucln=TAXMONEY]').val(
			parseFloat(total4).toFixed(2));
	$('#qtc_addWinForm').find('input[qucln=TAXMONEY]').attr("chan", "T");
	$('#qtc_addWinForm').find('input[qucln=TAXMONEY]').next().val(
			parseFloat(total4).toFixed(2));
	
}

//改变去向的值
function getgoabout(prefix){
	var STOCK_ID=$('#'+prefix+'_addWinForm').find('input[qucln=STOCK_ID]').val();
	return " man_id in('"+STOCK_ID+"','1E2660920B3DAFF9DFACFE66C37269E4')";
}

function getclxId(prefix){
	var currentRow = $('#qtc_addItemDatagrid').datagrid("getEditRow"); // 获取正在编辑的行
    var cl_hetong=currentRow["CL_HETONGHAO"];
    return " CL_HETONGHAO='"+cl_hetong+"'";
}

// 改变去向的值
function changeGoabout(){
}

function getyincixt(newVal,prefix,obj){
	var xtid=$(obj).attr("oldVal");   //选题
	var grid=$("div[realdsid][prefix][dsid]");
	var index=$(grid).datagrid("getEditRowIndex");
	var allRows=$(grid).datagrid("getRows");
	var rowData={};
	 var currentRow = $('#'+prefix+'_addItemDatagrid').datagrid("getEditRow"); // 获取正在编辑的行
	var cl_hetong=currentRow["CL_HETONGHAO"];  //合同
	var yinci;
	$.ajax({
		type:'post',
		url:_m_url+'fxa/getyinci',
		async : false,
		data:{'cl_hetong':cl_hetong,'xtid':xtid},
		success:function(data){
			if(data.length>0){
				yinci=data[0].YINCI;
			}
		} 
	});
	rowData["YINCI"]=yinci;
	$(grid).datagrid("endEdit",index);
	fillMxDz(allRows[index],rowData);
	$(grid).datagrid("beginEdit",index);
}